﻿using Single.Core.Service;
using Single.Sugar.Service;
using Single.Sys.Entity;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Text;
using Single.Encrypt;
using SqlSugar;

namespace Single.Sys.Service
{
    public class SysUserService : BaseService<SysUser>
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public ServiceResult SignIn(string username, string password)
        {
            password = EncryptHelper.Md5(password);

            var entity = rep.GetSingle(m => m.Username.Equals(username) && m.Password.Equals(password));

            if (entity != null)
            {
                if (entity.Status.Equals("ok"))
                {
                    return SuccessResult("登录成功", data: entity);
                }
                else { return FailureResult("用户被禁用"); }
            }
            else { return FailureResult("帐号或密码错误"); }
        }

        /// <summary>
        /// 后台管理列表
        /// </summary>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <param name="username"></param>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<SysUser> GetPageList(int pageNum, int pageSize, string username, string name, string status, out int total)
        {
            var query = rep.AsQueryable().OrderBy(m => m.CreateTime, OrderByType.Desc);

            query = query.WhereIF(!string.IsNullOrEmpty(username), m => m.Username.Contains(username))
                .WhereIF(!string.IsNullOrEmpty(name), m => m.Name.Contains(name))
                .WhereIF(!string.IsNullOrEmpty(status), m => m.Status.Contains(status));

            return query.ToPageList(pageNum, pageSize, out total);
        }

        /// <summary>
        /// 重写保存
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override ServiceResult InsertOrUpdate(SysUser entity)
        {
            if (entity.Id > 0)
            {
                var line = rep.AsUpdateable(entity)
                    .IgnoreColumns(m => new { m.Username, m.Password }).ExecuteCommand();

                return UnknownResult(line > 0);
            }
            else
            {
                entity.Password = EncryptHelper.Md5(entity.Password);

                return Insert(entity);
            }
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public ServiceResult ResetPassword(long id, string password)
        {
            password = EncryptHelper.Md5(password);

            var line = rep.AsUpdateable().SetColumns(m => new SysUser { Password = password }, true).Where(m => m.Id == id).ExecuteCommand();

            return UnknownResult(line > 0);
        }
    }
}